const request = require('request-promise')
const fs = require('fs-extra')
const {pick} = require('ramda')
const {booksFile, bookFields} = require('./config')

let booksFileChanged = false

const genUrl = page =>
      `https://www.shanbay.com/api/v1/read/book/?page=${page}&ipp=20&type=intensity&value=1&_=${Date.now()}`

const checkDataFiles = async () => {
  const booksFileExists = await fs.pathExists(booksFile)
  if(!booksFileExists) {
    await fs.ensureFile(booksFile)
    await fs.writeJson(booksFile, [])
  }
}

const processBook = async (book, booksInFile) => {
  console.log(`图书：${book.title}`)
  const picked = pick(bookFields, book)
  const idx = booksInFile.findIndex(bk => bk.id === picked.id)
  if(idx === -1) {
    booksInFile.push({...picked, created_at: new Date()})
    booksFileChanged = true
  } else {
    const oldBook = booksInFile[idx]
    if(oldBook.price !== picked.price) {
      console.log(`old: ${oldBook.price} -> new: ${picked.price}`)
      //将当前价格及日期放入历史记录
      let history = oldBook.history ? oldBook.history : []
      history.push({
        ...pick(['price', 'created_at'], oldBook)
      })
      oldBook.history = history
      oldBook.price = picked.price
      oldBook.created_at = new Date()
      booksFileChanged = true
    }
  }
}

const scrape = async () => {
  await checkDataFiles()
  const booksInFile = await fs.readJson(booksFile)

  const {data: {ipp, total, books}} = await request({uri: genUrl(1), json: true})
  const totalPages = Math.ceil(total / ipp)
  console.log(`共有${totalPages}页...`)
  console.log('抓取第1页的图书...')
  books.forEach(book => processBook(book, booksInFile))

  for(let num = 2; num <= totalPages; num++) {
    const {data: {books: pageBooks}} = await request({uri: genUrl(num), json: true})
    console.log(`抓取第${num}页的图书...`)
    pageBooks.forEach(book => processBook(book, booksInFile))
  }
  if(booksFileChanged) {
    await fs.writeJson(booksFile, booksInFile)
  }
}

module.exports = scrape
